package com.mall.b2c.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.ArrayUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import com.mall.b2c.dao.GoodsTagDao;
import com.mall.b2c.domain.goods.GoodsDomain;
import com.mall.b2c.domain.goods.GoodsTagDomain;
import com.mall.b2c.domain.goods.TagDomain;
import com.mall.b2c.service.GoodsService;
import com.mall.b2c.service.GoodsTagService;
import com.mall.b2c.service.TagService;
import com.mall.common.exception.NoticeException;
import com.mall.common.hibernate.Finder;
import com.mall.common.service.impl.BaseServiceImpl;


/**
 * Service实现类 - 商品标签
 */
@Service
@Transactional
public class GoodsTagServiceImpl extends BaseServiceImpl<GoodsTagDomain, Long> implements GoodsTagService {
	
	@Resource private TagService tagService;
	@Resource private GoodsService goodsService;
	
	@Resource
	public void setBaseDao(GoodsTagDao goodsTagDao) { 
		super.setBaseDao(goodsTagDao); 
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void correlate(Long tagId, Long[] goodsIds) {
		if(tagId == null || ArrayUtils.isEmpty(goodsIds)){
			throw new NoticeException("系统参数错误");
		}
		
		TagDomain tag = tagService.find(tagId);
		if(tag == null){
			throw new NoticeException("标签不存在");
		}
		
		List<GoodsDomain> goodes = goodsService.get(goodsIds);
		if(CollectionUtils.isEmpty(goodes)){
			throw new NoticeException("商品不存在");
		}
		boolean allCorrelated = true;
		int maxSort = getMaxSort(tagId);
		for(GoodsDomain bean : goodes){
			if(!isCorrelate(tag.getId(), bean.getId())){
				GoodsTagDomain goodsTag = new GoodsTagDomain();
				goodsTag.setPriority(++maxSort);
				goodsTag.setTag(tag);
				goodsTag.setGoods(bean);
				this.persist(goodsTag);
				allCorrelated = false;
			}
		}
		if(allCorrelated){
			throw new NoticeException("商品已经关联过了");
		}
	}
	
	private boolean isCorrelate(Long tagId, Long goodsId){
		Finder finder = Finder.create("from GoodsTagDomain bean where goods.id = :goodsId and tag.id = :tagId");
		finder.setParam("goodsId", goodsId);
		finder.setParam("tagId", tagId);
		Object obj = finder.findUniqueByHQL();
		return obj != null;
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void eidtSort(Long id, Integer sort) {
		if(id == null || sort == null){
			throw new NoticeException("系统参数错误");
		}
		
		GoodsTagDomain goodsTag = this.find(id);
		if(goodsTag == null){
			throw new NoticeException("商品标签不存在");
		}
		
		goodsTag.setPriority(sort);
		this.update(goodsTag);
	}
	
	private int getMaxSort(Long tagId){
		Finder finder = Finder.create("select max(bean.priority) from GoodsTagDomain bean where bean.tag.id = :tagId");
		finder.setParam("tagId", tagId);
		Object obj = finder.findUniqueByHQL();
		if(obj == null){
			return 0;
		}
		return (Integer) obj;
	}
}
